﻿using System;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using AstekServis.Web.Data;
using System.Collections.Generic;

namespace AstekServis.Views.Admin
{
    public partial class KullaniciTanitim : ChildWindow
    {
        AsTekDomainContext context = new AsTekDomainContext();

        public KullaniciTanitim()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(KullaniciTanitim_Loaded);
        }

        void KullaniciTanitim_Loaded(object sender, RoutedEventArgs e)
        {
            cmbFirma.Items.Add("Ayna Sanat");
            cmbFirma.Items.Add("Smart Media");
            cmbFirma.SelectedIndex = 0;
            LoadData();
        }

        private void LoadData()
        {
            EntityQuery<Kullanici> qry = context.GetKullanicilarQuery().OrderBy(x => x.TamAdi);
            LoadOperation<Kullanici> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<Kullanici> op = ss as LoadOperation<Kullanici>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                }
            };
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SaveRecord();
        }

        private void SaveRecord()
        {
            if (lblID.Content != null && !string.IsNullOrEmpty(txtKullaniciAdi.Text) && !string.IsNullOrEmpty(txtSifre.Text))
            {
                LoadOperation<Kullanici> operation = context.Load(context.GetKullanicilarQuery().Where(x => x.KullaniciAdi == txtKullaniciAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<Kullanici> op = ss as LoadOperation<Kullanici>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        Kullanici usr = op.Entities.FirstOrDefault();
                        if (usr != null)
                        {
                            usr.TamAdi = txtTamAdi.Text;
                            usr.Sifre = txtSifre.Text;
                            usr.SessionID = "0";
                            usr.KullaniciAdi = txtKullaniciAdi.Text;
                            usr.emailAdress = txtEmail.Text;
                            usr.YetkiSeviyesi = Cevir.Parse<byte>(chkYonetici.IsChecked);
                            usr.DbNo = cmbFirma.SelectedIndex;
                            usr.ServisKapatir = chkServis.IsChecked.Value;
                            usr.MontajKapatir = chkMontaj.IsChecked.Value;

                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            Kullanici newusr = new Kullanici();
                            newusr.TamAdi = txtTamAdi.Text;
                            newusr.Sifre = txtSifre.Text;
                            newusr.emailAdress = txtEmail.Text;
                            newusr.KullaniciAdi = txtKullaniciAdi.Text;
                            newusr.YetkiSeviyesi = Cevir.Parse<byte>(chkYonetici.IsChecked);
                            newusr.SessionID = "0";
                            newusr.DbNo = cmbFirma.SelectedIndex;
                            newusr.ServisKapatir = chkServis.IsChecked.Value;
                            newusr.MontajKapatir = chkMontaj.IsChecked.Value;

                            context.Kullanicis.Add(newusr);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadData();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void newButton_Click(object sender, RoutedEventArgs e)
        {
            txtTamAdi.Text = "";
            txtSifre.Text = "";
            txtEmail.Text = "";
            txtKullaniciAdi.Text = "";
            lblID.Content = "";
            cmbFirma.SelectedIndex = 0;
            chkYonetici.IsChecked = false;
            chkMontaj.IsChecked = false;
            chkServis.IsChecked = false;
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (lblID.Content != null && !string.IsNullOrEmpty(txtKullaniciAdi.Text) && !string.IsNullOrEmpty(txtSifre.Text))
            {
                LoadOperation<Kullanici> operation = context.Load(context.GetKullanicilarQuery().Where(x => x.KullaniciAdi == txtKullaniciAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<Kullanici> op = ss as LoadOperation<Kullanici>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        Kullanici usr = op.Entities.FirstOrDefault();
                        context.Kullanicis.Remove(usr);
                        context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadData();
                                }
                            }, null);
                    }
                };
            }
        }

        private void agDataGrid1_RowClick(object sender, DevExpress.AgDataGrid.RowClickEventArgs e)
        {
            Kullanici usr = (Kullanici)agDataGrid1.FocusedDataRow;
            if (usr != null)
            {
                txtTamAdi.Text = usr.TamAdi;
                txtSifre.Text = usr.Sifre;
                txtEmail.Text = usr.emailAdress;
                txtKullaniciAdi.Text = usr.KullaniciAdi;
                lblID.Content = usr.ID.ToString();
                cmbFirma.SelectedIndex = usr.DbNo;
                chkYonetici.IsChecked = Cevir.Parse<bool>(usr.YetkiSeviyesi);
                chkServis.IsChecked = usr.ServisKapatir;
                chkMontaj.IsChecked = usr.MontajKapatir;
            }
        }
    }
}

